package org.microservice.common.oss;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.microservice.common.util.R;

import java.io.File;
import java.io.InputStream;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/8/23
 * Time: 15:19
 * To change this template use File | Settings | File Templates.
 * Description:
 */
public class QiniuCloudStorage {

    private UploadManager uploadManager;

    private String token;

    public QiniuCloudStorage(QiniuCloudStorageProperties qiniuCloudStorageProperties) {

        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone0());

        this.uploadManager = new UploadManager(cfg);

        Auth auth = Auth.create(qiniuCloudStorageProperties.getAccessKey(), qiniuCloudStorageProperties.getSecretKey());
        this.token = auth.uploadToken(qiniuCloudStorageProperties.getBucket());

    }


    public static QiniuCloudStorage build(QiniuCloudStorageProperties qiniuCloudStorageProperties){
        return new QiniuCloudStorage(qiniuCloudStorageProperties);
    }


    public R upload(String filePath, String key){
        try {
            Response response  = this.uploadManager.put(filePath, key, this.token);
            return checkResponse(response);

        } catch (QiniuException e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }

    }

    public R upload(byte[] bytes, String key){
        try {
            Response response = this.uploadManager.put(bytes, key, this.token);

            return checkResponse(response);

        } catch (QiniuException e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    public R upload(InputStream inputStream, String key) {
        try {
            Response response = this.uploadManager.put(inputStream, key, this.token,null,null);
           return checkResponse(response);
        } catch (QiniuException e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    public R upload(File file, String key) {
        try {
            Response response = this.uploadManager.put(file, key, this.token);
            return checkResponse(response);
        } catch (QiniuException e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }


    private R checkResponse(Response response){
        //解析上传成功的结果
        try {
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            int statusCode = response.statusCode;
            if(statusCode == 200){
                return R.ok().put("hash",putRet.hash).put("key",putRet.key);
            }else{
                return R.error(response.bodyString());
            }
        } catch (QiniuException e) {
            e.printStackTrace();

            return R.error(e.getMessage());
        }
    }
}
